package ru.testtask.model;

import java.util.LinkedList;
import java.util.List;

public class XmlElement {

    private static final String TEN_TABS = "\t\t\t\t\t\t\t\t\t\t\t";

    private int id;
    private String name;
    private XmlAttribute attribute = new XmlAttribute();

    private XmlElement parent;
    private List<XmlElement> childs;

    private int level;

    public XmlElement(int id) {
        this.id = id;
    }

    public XmlElement(String name) {
        this.name = name;
        this.level = 0;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getLevel() {
        return level;
    }

    public XmlAttribute getAttribute() {
        return attribute;
    }

    public void setParent(XmlElement parent) {
        setParent(parent, true);
    }

    public void setParent(XmlElement parent, boolean changeLevel) {
        this.parent = parent;
        if (changeLevel) {
            setLevel(parent.level + 1);
        }
    }

    public XmlElement getParent() {
        return parent;
    }

    public void addChild(XmlElement child) {
        addChild(child, true);
    }

    public void addChild(XmlElement child, boolean changeLevel) {
        child.setParent(this, changeLevel);
        if (childs == null) {
            childs = new LinkedList<XmlElement>();
        }
        childs.add(child);
    }

    public List<XmlElement> getChilds() {
        return childs;
    }

    @Override
    public String toString() {
        return "XmlElement{" +
                "name='" + name + '\'' +
                ", attribute=" + attribute +
                '}' +
                "childs[" + ((childs != null) ? childs.size() : 0) + "]";
    }


    public String printOpenTag() {
        StringBuilder result = new StringBuilder();
        if (level < 10) {
            for (int i=0; i<level; i++) {
                result.append('\t');
            }
        } else {
            result.append(TEN_TABS);
        }
        result.append("<").append(name).append(" ").
                append((attribute != null) ? attribute.printXmlAttribute() : "").append(">" + "\n");
        return result.toString();
    }

    public String printCloseTag() {
        StringBuilder result = new StringBuilder();
        if (level < 10) {
            for (int i=0; i<level; i++) {
                result.append('\t');
            }
        } else {
            result.append(TEN_TABS);
        }
        result.append("</").append(name).append(">" + "\n");
        return result.toString();
    }


    /**
     * this method may throw StackOverFlowError
     * So use {@link ru.testtask.service.XmlService#transformObjectModelToXmlString(XmlDocument)}
     * @return xml element with content containing sub-elements
     */
    public String printXmlElement() {
        StringBuilder format = new StringBuilder();
        for (int i=0; i<level; i++) {
            format.append('\t');
        }
        StringBuilder result = new StringBuilder();
        result.append(format).append("<").append(name).append(" ").
                append((attribute != null) ? attribute.printXmlAttribute() : "").append(">" + "\n");
        if (childs != null) {
            for (XmlElement child : childs) {
                result.append(child.printXmlElement());
            }
        }
        result.append(format).append("</").append(name).append(">" + "\n");
        return result.toString();
    }

}
